<?php

/**
 * Model MyEmail
 *
 * @author nguyenmanhluu
 * @since Jul 29, 2013 - 9:48:21 PM
 */
class MyEmail
{

    const SECRET_KEY = 'nqUo21nQ7H7RsCK8Jpef';

    /**
     * Các loại Mail
     */
    //Account

    const TYPE_NEW_CUSTOMER                      = 1;
    const TYPE_NEW_BIZOWNER                      = 2;
    const TYPE_NEW_STAFF                         = 3;
    const TYPE_PASS_RECOVER                      = 4;
    const TYPE_CUSTOMER_PASSWORD_RECOVER_CONFIRM = 19;
    const TYPE_ACCOUNT_BLOCK                     = 5;
    const TYPE_ACCOUNT_DELETE                    = 6;
    const TYPE_NEW_SERVICE                       = 8;
//    const TYPE_CUSTOMER_ACTIVE = 21;
////    const TYPE_CUSTOMER_BLOCK = 20;
    //New Appoint
    const TYPE_NEW_APPOINT_NOTI_CUS              = 7; //Order confirm mail to customer
    const TYPE_BOOKING_CONFIRM_LINK              = 9; //Order confirm code to customer
    const TYPE_NEW_APPOINT_NOTI_BIZ              = 10; //Notifi about New Appoint to Merchant

    /**
     * @var type Name in setting: New Booking Email To Merchant's Staff
     * 
     * When a customer confirm service, webmaster will send this temp to staff
     */
    const TYPE_NEW_APPOINT_NOTI_STAFF                   = 11; //Notifi about New Appoint to Staff
    //
    //Change appoint
    const TYPE_CHANGE_APPOINT_NOTI_STAFF                = 12; //Notifi about appoint change to Staff
    const TYPE_CHANGE_APPOINT_NOTI_BIZ                  = 13; //Notifi about appoint change to Merchant
    const TYPE_CHANGE_APPOINT_NOTI_CUS                  = 14; //Notifi about appoint change to Customer
    //Cancel appoint
    const TYPE_CANCEL_APPOINT_NOTI_STAFF                = 15; //Order change mail to staff
    const TYPE_CANCEL_APPOINT_NOTI_BIZ                  = 16; //Order change mail to staff
    const TYPE_CANCEL_APPOINT_NOTI_CUS                  = 17; //Order change mail to staff
    const TYPE_RECONFIRM_BOOKING                        = 18; //Order change mail to staff
    //Review
    const TYPE_INVITE_REVIEW                            = 30; //Invite to reviews service
    const TYPE_REJECT_BOOKING_TO_CUSTOMER               = 31;
    const TYPE_BOOKING_REQUEST_NOTIFICATION_TO_CUSTOMER = 20;
    const TYPE_BOOKING_TO_CONCEIRGE                     = 21;
    const TYPE_CONFIRMED_REQUEST_BOOKING                = 32;
    const TYPE_CONTACT_TO_ADMIN                         = 33;
    const TYPE_CONTACT_TO_CUSTOMER                      = 34;
    const TYPE_REQUEST_BOOKING_TO_ADMIN                 = 35;
    const TYPE_HOTDEAL_BOOKING_CONFIRMED_TO_CUSTOMER    = 36;
    //Merchant forgot password
    const TYPE_MERCHANT_PASSWORD_RECOVER_CONFIRM        = 38;
    const TYPE_MERCHANT_PASSWORD_RESET                  = 37;

    /**
     * Các loại code hỗ trợ
     */
    const CODE_USER_NAME                   = '[user_name]';
    const CODE_USER_TITLE                  = '[user_title]';
    const CODE_PASSWORD                    = '[password]';
    const CODE_LINK                        = '[link]';
    const CODE_EMAIL                       = '[email]';
    const CODE_BOOKING_CONFIRM_LINK        = '[booking_confirm_link]';
    const CODE_PASSWORD_RESET_CONFIRM_LINK = '[password_reset_confirm_link]';
    const CODE_BUSINESS                    = '[business]';
    const CODE_BUSINESS_URL                = '[business_url]';

    /*
     * Code cho Order Confirm
     */
    const CUSTOMER_NAME          = '[customer_name]';
    const CUSTOMER_PHONE         = '[customer_phone]';
    const CUSTOMER_EMAIL         = '[customer_email]';
    const ORDER_NOTE             = '[order_note]';
    const ORDER_DATE             = '[order_date]';
    const ORDER_TIME             = '[order_time]';
    const ORDER_SERVICE          = '[order_service]';
    const ORDER_STAFF            = '[order_staff]';
    const ORDER_PRICE            = '[order_price]';
    const ORDER_SERVICE_LNG      = '[order_service_lng]';
    const ORDER_SERVICE_LAT      = '[order_service_lat]';
    const ORDER_SERVICE_ADDRESS  = '[order_service_address]';
    const ORDER_SERVICE_IMAGE    = '[order_service_image]';
    const ORDER_TAX              = '[order_tax]';
    const ORDER_DISCOUNT         = '[order_discount]';
    const ORDER_TOTAL_MONEY      = '[order_total_money]';
    const REVIEWS_SERVICE_LINK   = '[reviews_service_link]';
    const MERCHANT_CONTACT_PHONE = '[merchant_contact_number]';
    const LINK_MAP               = '[link_map]';
    const TIME_SLOT_CONFIRMED    = '[requested_time_slot_confirmed]';
    const REDEMPTION_CODE        = '[redemption_code]';

    /**
     * Booking
     */
    const REQUEST_TIME_1 = '[time_slot_1]';
    const REQUEST_TIME_2 = '[time_slot_2]';
    const REQUEST_TIME_3 = '[time_slot_3]';

    /**
     * Loại Mail
     * @var int 
     */
    public $type;
    public $params       = array();
    public $from         = '';
    public $fromName     = '';
    static $arr_email_cc = FALSE;
    public $arr_cc_email = array();

    /**
     *
     * @var string 
     */
    public $to               = '';
    public $subject          = '';
    public $body             = '';
    public $file_attach      = '';
    public $title_mail_order = '';

    protected function __setUp()
    {
        $content = EmailSetting::model()->findByPk($this->type);

        $this->from     = Yii::app()->params['fromMail'];
        $this->fromName = Yii::app()->params['fromName'];

        // Tạo footer cho mail với
        // link bỏ theo dõi và quản lý ở chân email
//        $unsubscribe = self::getUnsubcribeLink($this->to);
//        $manageLink = Yii::app()->createAbsoluteUrl('subscribe/manage');
//        $footer = "<div style='color:#999999;font-size:10px;text-align:center;margin:10px;'>";
//        $footer .= "<hr/>This email was sent to $this->to <br/>";
//        $footer .= "<a href='$unsubscribe'>Unsubscribe</a>.";
//        $footer .= "Change your email settings at <a href='$manageLink'>Manage Subscriptions.</a>";
//        $footer .= "</div>";
        $footer = '';

        // Load mẫu email từ csdl
        // Nếu không phải là nội dung lấy từ EmailSetting
        if ($content == null)
        {
            $body       = $this->body . $footer;
            $title_mail = $this->title_mail_order;
        }
        else
        {
            // Nếu là nội dung lấy từ EmailSetting
            $body       = $content->letter_content . $footer;
            $title_mail = $content->letter_title;
        }
        // Tạo nội dung của email
        $search        = array_keys($this->params);
        $this->body    = str_replace($search, $this->params, $body);
        $this->subject = str_replace($search, $this->params, $title_mail);
    }

    public static function getUnsubcribeLink($email)
    {
        return Yii::app()->createAbsoluteUrl('subscribe/unsubscribe', array(
                    'email' => $email,
                    'token' => self::getToken($email)
        ));
    }

    public static function getToken($email)
    {
        $tokenSalt = 'voithan-voithan-voithan-voithan';
        return md5($tokenSalt . $email . $tokenSalt);
    }

    function send()
    {
        $this->__setUp();

        if (Yii::app()->params['SendBySMTP'])
        {
            Yii::trace("SMTP: $this->to, $this->subject, $this->body", 'mail');
            return $this->_smtp($this->to, $this->subject, $this->body);
        }
        else
        {
            Yii::trace("mail(): $this->to, $this->subject, $this->body", 'mail');
            return $this->__mail($this->to, $this->subject, $this->body);
        }
    }

    /**
     * send email to contact
     * @param CACtive record $model_contact A activerecord of contact
     */
    public static function send_new_contact_to_user($model_contact)
    {
        $mail         = new MyEmail();
        $mail->to     = $model_contact->email; //email admin
        $mail->type   = MyEmail::TYPE_CONTACT_TO_CUSTOMER;
        $mail->params = array(
            MyEmail::CODE_USER_NAME => $model_contact->first_name,
        );

        ob_start();
        $mail->send();
        ob_clean();
    }

    public static function send_new_contact_to_admin($model_contact)
    {
        $mail         = new MyEmail();
        $mail->to     = Yii::app()->params['email_contact'];
        $mail->type   = MyEmail::TYPE_CONTACT_TO_ADMIN;
        $mail->params = array(
            MyEmail::CODE_USER_NAME  => $model_contact->first_name,
            MyEmail::CODE_EMAIL      => $model_contact->email,
            MyEmail::CODE_USER_TITLE => $model_contact->title,
        );

        ob_start();
        $mail->send();
        ob_clean();
    }

    public static function send_error($msg)
    {
        $email = new MyEmail();

        $email->to           = 'anhduc.bkhn@gmail.com';
        $email->arr_cc_email = array('nghiemhiep18@gmail.com');
        $email->body         = $msg;
        $email->send();
    }

    function easySend()
    {
        $this->from     = Yii::app()->params['fromMail'];
        $this->fromName = Yii::app()->params['fromName'];
        if (Yii::app()->params['SendBySMTP'])
        {
            return $this->_smtp($this->to, $this->subject, $this->body);
        }
        else
        {
            $this->__mail($this->to, $this->subject, $this->body);
        }
    }

    protected function __mail($to, $subject, $body)
    {
        $headers = "From: {$this->from}\r\nReply-To: {$this->from}";
        mail($to, $subject, $body, $headers);
    }

    protected function _smtp($to, $subject, $body)
    {


        Yii::import('application.extensions.phpmailer.JPhpMailer');

        $mail             = new JPhpMailer;
        $mail->IsSMTP();
        $mail->Host       = Yii::app()->params['smtpHost'];
        $mail->Port       = Yii::app()->params['smtpPort'];
        $mail->SMTPAuth   = Yii::app()->params['smtpAuth'];
        $mail->Username   = Yii::app()->params['smtpUsername'];
        $mail->Password   = Yii::app()->params['smtpPassword'];
        $mail->SMTPSecure = Yii::app()->params['smtpSecure'];

        if ($this->arr_cc_email)
        {
            foreach ($this->arr_cc_email as $value)
            {
                $mail->AddCC($value);
            }
        }
        else
        {
//            echo 'hhh';
        }

//        die();

        $mail->SetFrom($this->from, $this->fromName);

        if ($this->file_attach)
        {
            $mail->AddAttachment($this->file_attach);
        }
        $mail->Subject = $subject;
        $mail->AltBody = 'To read full content, please view as HTML method';

        $mail->MsgHTML($body);
        $mail->AddAddress($to);

        return $mail->Send();
    }

    static public function getAllCode()
    {
        return array(
            self::CODE_USER_NAME,
            self::CODE_USER_TITLE,
            self::CODE_PASSWORD,
            self::CODE_LINK,
            self::CODE_EMAIL,
            self::CODE_BOOKING_CONFIRM_LINK,
            self::CODE_BUSINESS,
            self::CUSTOMER_NAME,
            self::CUSTOMER_PHONE,
            self::CUSTOMER_EMAIL,
            self::ORDER_NOTE,
            self::ORDER_TIME,
            self::ORDER_DATE,
            self::ORDER_SERVICE,
            self::ORDER_STAFF,
            self::ORDER_PRICE,
            self::ORDER_SERVICE_LNG,
            self::ORDER_SERVICE_LAT,
            self::ORDER_SERVICE_ADDRESS,
            self::ORDER_SERVICE_IMAGE,
            self::ORDER_TAX,
            self::ORDER_DISCOUNT,
            self::ORDER_TOTAL_MONEY,
            self::REVIEWS_SERVICE_LINK,
            self::REQUEST_TIME_1,
            self::REQUEST_TIME_2,
            self::REQUEST_TIME_3,
            self::MERCHANT_CONTACT_PHONE,
            self::REDEMPTION_CODE,
        );
    }

    /**
     * send email to all conceirge
     */
    public static function send_booking_new_request_to_conceirge($model_request)
    {

        $crite         = new CDbCriteria();
        $crite->select = array('email', 'type');
        $crite->compare('type', Users::TYPE_CONCIERGE);

        $conceirge = Users::model()->findAll($crite);

        if ($conceirge)
        {
            $arr = array();
            foreach ($conceirge as $value)
            {
//                self::$arr_email_cc = $value->email;
                $arr[] = $value->email;
            }

            $mail               = new MyEmail();
            $mail->arr_cc_email = $arr;
//            $mail->to ='anhduc.bkhn@dotsymedia.com';
            $mail->to           = Yii::app()->params['email_contact'];
            $mail->type         = MyEmail::TYPE_BOOKING_TO_CONCEIRGE;
            $mail->params       = array(
                MyEmail::CUSTOMER_EMAIL         => $model_request->customer->user->email,
                MyEmail::CUSTOMER_NAME          => $model_request->customer->user->getName(),
                MyEmail::CUSTOMER_PHONE         => $model_request->customer->user->getPhone(),
                MyEmail::ORDER_NOTE             => $model_request->note,
                MyEmail::ORDER_SERVICE          => $model_request->service_name,
                MyEmail::CODE_BUSINESS          => $model_request->merchant->business_name,
                MyEmail::REQUEST_TIME_1         => $model_request->request_time_1,
                MyEmail::REQUEST_TIME_2         => $model_request->request_time_2,
                MyEmail::REQUEST_TIME_3         => $model_request->request_time_3,
                MyEmail::ORDER_SERVICE_ADDRESS  => $model_request->merchant->address,
                MyEmail::MERCHANT_CONTACT_PHONE => $model_request->merchant->getPhone(),
//                MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $model_request->merchant->postal_code,
                MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$model_request->merchant->latitude},{$model_request->merchant->longitude}"
            );
            ob_start();
            $mail->send();
            ob_clean();
        }
        else
        {

            return FALSE;
        }
    }

    public static function send_booking_request_confirm_customer($model_request)
    {
        $mail         = new MyEmail();
        $mail->to     = $model_request->customer->user->email;
        $mail->type   = MyEmail::TYPE_CONFIRMED_REQUEST_BOOKING;
        $mail->params = array(
            MyEmail::CUSTOMER_EMAIL         => $model_request->customer->user->email,
            MyEmail::CUSTOMER_NAME          => $model_request->customer->user->getName(),
            MyEmail::ORDER_SERVICE_IMAGE    => $model_request->merchant->getPublicPhoto(),
            MyEmail::CUSTOMER_PHONE         => $model_request->customer->user->getPhone(),
            MyEmail::ORDER_NOTE             => $model_request->note,
            MyEmail::ORDER_SERVICE_ADDRESS  => $model_request->merchant->address,
            MyEmail::ORDER_SERVICE          => $model_request->service_name,
            MyEmail::CODE_BUSINESS          => $model_request->merchant->business_name,
            MyEmail::TIME_SLOT_CONFIRMED    => $model_request->getTimeConfirm(),
            MyEmail::MERCHANT_CONTACT_PHONE => $model_request->merchant->getPhone(),
//            MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $model_request->merchant->postal_code,
            MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$model_request->merchant->latitude},{$model_request->merchant->longitude}"
        );

        ob_start();
        $mail->send();
        ob_clean();
    }

    /**
     * Send an email to customer when customer booking a request
     */
    public static function send_booking_request_to_customer($model_request)
    {
        $mail         = new MyEmail();
        $mail->to     = $model_request->customer->user->email;
        $mail->type   = MyEmail::TYPE_BOOKING_REQUEST_NOTIFICATION_TO_CUSTOMER;
        $mail->params = array(
            MyEmail::CUSTOMER_EMAIL        => $model_request->customer->user->email,
            MyEmail::CUSTOMER_NAME         => $model_request->customer->user->getName(),
            MyEmail::ORDER_SERVICE_IMAGE   => $model_request->merchant->getPublicPhoto(),
            MyEmail::CUSTOMER_PHONE        => $model_request->customer->user->getPhone(),
            MyEmail::ORDER_NOTE            => $model_request->note,
            MyEmail::ORDER_SERVICE_ADDRESS => $model_request->merchant->address,
            MyEmail::ORDER_SERVICE         => $model_request->service_name,
            MyEmail::CODE_BUSINESS         => $model_request->merchant->business_name,
            MyEmail::REQUEST_TIME_1        => $model_request->request_time_1,
            MyEmail::REQUEST_TIME_2        => $model_request->request_time_2,
            MyEmail::REQUEST_TIME_3        => $model_request->request_time_3,
//            MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $model_request->merchant->postal_code,
            MyEmail::LINK_MAP              => "http://maps.google.com/maps?q={$model_request->merchant->latitude},{$model_request->merchant->longitude}"
        );

        ob_start();
        $mail->send();
        ob_clean();
    }

    /**
     * Send email to customer when rejecting request booking
     * @param type $model_request
     */
    public static function send_booking_reject_to_customer($model_request)
    {

        $mail         = new MyEmail();
        $mail->to     = $model_request->customer->user->email;
        $mail->type   = MyEmail::TYPE_REJECT_BOOKING_TO_CUSTOMER;
        $mail->params = array(
            MyEmail::CODE_USER_NAME => $model_request->customer->user->getName(),
            MyEmail::ORDER_SERVICE  => $model_request->service_name,
            MyEmail::CODE_BUSINESS  => $model_request->merchant->business_name,
            MyEmail::REQUEST_TIME_1 => $model_request->request_time_1,
            MyEmail::REQUEST_TIME_2 => $model_request->request_time_2,
            MyEmail::REQUEST_TIME_3 => $model_request->request_time_3
        );

        ob_start();
        $mail->send();
        ob_clean();
    }

    public static function sendBlockMerchant($model)
    {

        $mail         = new MyEmail();
        $mail->type   = MyEmail::TYPE_ACCOUNT_BLOCK;
        $mail->to     = $model->email;
        $mail->params = array(
            MyEmail::CODE_EMAIL      => $model->email,
            MyEmail::CODE_USER_NAME  => $model->name,
            MyEmail::CODE_USER_TITLE => $model->name,
            MyEmail::CODE_BUSINESS   => $model->business_name
        );
        ob_start();
        $mail->send();
        Yii::log(ob_get_contents(), 'error', 'SendMail');
        ob_clean();
    }

    public static function sendBlockCustomer($model, $biz_name, $biz_url)
    {

        $mail         = new MyEmail();
        $mail->type   = MyEmail::TYPE_ACCOUNT_BLOCK;
        $mail->to     = $model->email;
        $mail->params = array(
            MyEmail::CODE_EMAIL        => $model->email,
            MyEmail::CODE_USER_NAME    => $model->name,
            MyEmail::CODE_BUSINESS     => $biz_name,
            MyEmail::CODE_BUSINESS_URL => $biz_url
        );

        ob_start();
        $mail->send();
        Yii::log(ob_get_contents(), 'error', 'SendMail');
        ob_clean();
    }

    public static function sendActiveCustomer($model, $biz_name, $biz_url)
    {

        $mail         = new MyEmail();
        $mail->type   = MyEmail::TYPE_NEW_CUSTOMER;
        $mail->to     = $model->email;
        $mail->params = array(
            MyEmail::CODE_EMAIL        => $model->email,
            MyEmail::CODE_USER_NAME    => $model->name,
            MyEmail::CODE_USER_TITLE   => $model->name,
            MyEmail::CODE_BUSINESS     => $biz_name,
            MyEmail::CODE_BUSINESS_URL => $biz_url
        );

        ob_start();
        $mail->send();
        Yii::log(ob_get_contents(), 'error', 'SendMail');
        ob_clean();
    }

    /**
     * Send welcome mail to new user
     * @param ActiveRecord $model User model want to mail
     * @param string $newPwd Pasword of new user
     */
    public static function sendToNewUser($model, $newPwd)
    {
        $mail = new MyEmail();

        $sendmail = true;
        switch ($model->type)
        {
            case Users::TYPE_CUSTOMER:
                $mail->type = MyEmail::TYPE_NEW_CUSTOMER;
                break;
            case Users::TYPE_STAFF:
                $mail->type = MyEmail::TYPE_NEW_STAFF;
                break;
            case Users::TYPE_BUSINESS:
                $mail->type = MyEmail::TYPE_NEW_BIZOWNER;
                break;
            case Users::TYPE_ADMIN:
                /**
                 * @VOITHAN TYPE_NEW?
                 */
                $mail->type = MyEmail::TYPE_NEW_;
                break;
            default:
                $sendmail   = false;
                break;
        }

        if ($sendmail)
        {
            $mail->to     = $model->email;
            $mail->params = array(
                MyEmail::CODE_EMAIL      => $model->email,
                MyEmail::CODE_USER_NAME  => $model->name,
                MyEmail::CODE_PASSWORD   => $newPwd,
                MyEmail::CODE_USER_TITLE => $model->name,
            );
            if ($model->type == Users::TYPE_BUSINESS)
            {
                $mail->params[MyEmail::CODE_BUSINESS] = $model->author->author ? $model->author->author->business_name : '';
            }

            ob_start();
            $mail->send();
            Yii::log(ob_get_contents(), 'error', 'SendMail');
            ob_clean();
        }
    }

    public function addAttach($str_path_file)
    {
        $this->file_attach = $str_path_file;
    }

    /*
     * Notify new appoint to Customer
     */

    public static function NewAppointToCus($appoint, $customer, $isc_file)
    {
        $mail = new MyEmail();

        $sendmail   = true;
        $mail->type = self::TYPE_NEW_APPOINT_NOTI_CUS;

        if ($sendmail)
        {
            $mail->to = $customer->user->email;
            $mail->addAttach($isc_file);

//            echo $appoint->apt_id;
//            die;
//            $order = @$appoint->order;
//            $order_tax = $order->tax ? $order->tax : 0;
//            $tax_money = round($order_tax * $appoint->service->price / 100, 2);
//            $tax_money = number_format($tax_money, 2);
//            $tax = '$' . $tax_money . ' (' . $order_tax . '%)';
//            $order_discount = $order->hotdeal_discount ? $order->hotdeal_discount : 0;
//            $discount_money = round($order_discount * $appoint->service->price / 100, 2);
//            $discount_money = number_format($discount_money, 2);
//            $char = $discount_money == 0 ? '' : '-';
//            $discount = '$' . $discount_money . " ($char" . $order_discount . '%)';
//            $total = number_format($appoint->service->price - $discount_money + $tax_money, 2);

            $mail->params = array(
                MyEmail::CUSTOMER_NAME          => $customer->getName(),
                MyEmail::CUSTOMER_PHONE         => $customer->getPhone(),
                MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
                MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
                MyEmail::ORDER_NOTE             => $appoint->note,
                MyEmail::ORDER_DATE             => $appoint->formatDate(),
                MyEmail::ORDER_TIME             => $appoint->startTime,
                MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
                MyEmail::ORDER_STAFF            => $appoint->author->getName(),
                MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_LNG      => $appoint->service->longitude,
                MyEmail::ORDER_SERVICE_LAT      => $appoint->service->latitude,
                MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
                MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
                MyEmail::ORDER_TAX              => '0', //$tax,
                MyEmail::ORDER_DISCOUNT         => '0', // $discount,
                MyEmail::ORDER_TOTAL_MONEY      => $appoint->service->getPriceFormat(),
//                MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
                MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}"
            );

            ob_start();
            $mail->send();
            Yii::log(ob_get_contents(), 'error', 'SendMail');
            ob_clean();
        }
    }

    /*
     * Notify new appoint to Biz
     */

    public static function NewAppointToBiz($appoint, $customer, $isc_file)
    {
        $mail = new MyEmail();

        $sendmail   = true;
        $mail->type = MyEmail::TYPE_NEW_APPOINT_NOTI_BIZ;

        if ($sendmail)
        {
            //Mail to Biz
            $mail->to = $appoint->service->user->email;
            $mail->addAttach($isc_file);

//            $order = @$appoint->order;
//            $order_tax = $order->tax ? $order->tax : 0;
//            $tax_money = round($order_tax * $appoint->service->price / 100, 2);
//            $tax_money = number_format($tax_money, 2);
//            $tax = '$' . $tax_money . ' (' . $order_tax . '%)';
//            $order_discount = $order->hotdeal_discount ? $order->hotdeal_discount : 0;
//            $discount_money = round($order_discount * $appoint->service->price / 100, 2);
//            $discount_money = number_format($discount_money, 2);
//            $char = $discount_money == 0 ? '' : '-';
//            $discount = '$' . $discount_money . " ($char" . $order_discount . '%)';
//            $total = number_format($appoint->service->price - $discount_money + $tax_money, 2);

            $mail->params = array(
                MyEmail::CUSTOMER_NAME         => $customer->getName(),
                MyEmail::CUSTOMER_PHONE        => $customer->getPhone(),
                MyEmail::CUSTOMER_EMAIL        => $customer->user->email,
                MyEmail::ORDER_NOTE            => $appoint->note,
                MyEmail::ORDER_DATE            => $appoint->formatDate(),
                MyEmail::ORDER_TIME            => $appoint->startTime,
                MyEmail::ORDER_SERVICE         => $appoint->service->s_name,
                MyEmail::ORDER_STAFF           => $appoint->author->getName(),
                MyEmail::ORDER_PRICE           => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_LNG     => $appoint->service->longitude,
                MyEmail::ORDER_SERVICE_LAT     => $appoint->service->latitude,
                MyEmail::ORDER_SERVICE_IMAGE   => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS => $appoint->service->address,
                MyEmail::CODE_BUSINESS         => $appoint->service->user->business_name,
                MyEmail::ORDER_TAX             => '0', //$tax,
                MyEmail::ORDER_DISCOUNT        => '0', // $discount,
                MyEmail::ORDER_TOTAL_MONEY     => $appoint->service->getPriceFormat(), // $total,
            );

            ob_start();
            $mail->send();
            Yii::log(ob_get_contents(), 'error', 'SendMail');
            ob_clean();
        }
    }

    public static function send_email_confirmed_to_merchant($appoint, $customer, $isc_file, $discount, $total_money)
    {
        $mail = new MyEmail();

        $mail->type = MyEmail::TYPE_NEW_APPOINT_NOTI_BIZ;

        //email to customer
        $mail->to = $appoint->service->user->email;
        //$mail->to='anhduc.bkhn@gmail.com';
        if (FALSE != $isc_file)
        {
            $mail->addAttach($isc_file);
        }

        $mail->params = array(
            MyEmail::CUSTOMER_NAME          => $customer->user->getName(),
            MyEmail::CUSTOMER_PHONE         => $customer->user->getPhone(),
            MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
            MyEmail::ORDER_NOTE             => $appoint->note,
            MyEmail::ORDER_DATE             => $appoint->formatDate(),
            MyEmail::ORDER_TIME             => $appoint->startTime,
            MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
            MyEmail::ORDER_STAFF            => $appoint->author->getName(),
            MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
            MyEmail::ORDER_SERVICE_LNG      => $appoint->service->longitude,
            MyEmail::ORDER_SERVICE_LAT      => $appoint->service->latitude,
            MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
            MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
            MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
            MyEmail::ORDER_TAX              => '0', // $tax,
            MyEmail::ORDER_DISCOUNT         => $discount, // $discount,
            MyEmail::ORDER_TOTAL_MONEY      => $total_money, // $total
            MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
//            MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
            MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}"
                //MyEmail::REDEMPTION_CODE => Utils::genRandomString(),
        );
        ob_start();
        $mail->send();
        ob_clean();
    }

    /**
     * Send hotdeal confirm to staff
     * @param type $appoint
     * @param type $customer
     * @param type $isc_file
     * @param type $discount
     * @param type $total_money
     */
    public static function send_email_confirmed_to_staff($appoint, $customer, $isc_file, $discount, $total_money)
    {

        $mail = new MyEmail();

        $mail->type = MyEmail::TYPE_NEW_APPOINT_NOTI_STAFF;

        //email to customer
        $mail->to = $appoint->author->email;

        if (FALSE != $isc_file)
        {
            $mail->addAttach($isc_file);
        }

        $mail->params = array(
            MyEmail::CUSTOMER_NAME          => $customer->user->getName(),
            MyEmail::CUSTOMER_PHONE         => $customer->user->getPhone(),
            MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
            MyEmail::ORDER_NOTE             => $appoint->note,
            MyEmail::ORDER_DATE             => $appoint->formatDate(),
            MyEmail::ORDER_TIME             => $appoint->startTime,
            MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
            MyEmail::ORDER_STAFF            => $appoint->author->getName(),
            MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
            MyEmail::ORDER_SERVICE_LNG      => $appoint->service->longitude,
            MyEmail::ORDER_SERVICE_LAT      => $appoint->service->latitude,
            MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
            MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
            MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
            MyEmail::ORDER_TAX              => '0', // $tax,
            MyEmail::ORDER_DISCOUNT         => $discount, // $discount,
            MyEmail::ORDER_TOTAL_MONEY      => $total_money, // $total
            MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
//            MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
            MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}"
                //MyEmail::REDEMPTION_CODE => Utils::genRandomString(),
        );
        ob_start();
        $mail->send();
        ob_clean();
    }

    /**
     * Send email to customer when payment successfully
     * Use for hotdeal
     * @param type $appoint
     * @param type $customer
     * @param type $isc_file
     */
    public static function send_email_confirmed_to_customer($appoint, $customer, $isc_file, $discount, $total_money, $options = FALSE)
    {
        $mail = new MyEmail();

        $mail->type = MyEmail::TYPE_HOTDEAL_BOOKING_CONFIRMED_TO_CUSTOMER;

        //email to customer
        $mail->to = $customer->user->email;

        $redeemcode = strtoupper(Utils::genRandomString(4));

        while (TRUE)
        {
//            $redeemcode = strtolower(Utils::genRandomString(4));

            $is_exist = Redeem::model()->findAllByAttributes(array('redeem_code' => $redeemcode, 'biz_id' => $appoint->service->user->id));

            if (empty($is_exist))
            {
                break;
            }
            else
            {
                $redeemcode = strtoupper(Utils::genRandomString(4));
            }
        }

        if (FALSE != $isc_file)
        {
            $mail->addAttach($isc_file);
        }

        $mail->params = array(
            MyEmail::CUSTOMER_NAME          => $customer->user->getName(),
            MyEmail::CUSTOMER_PHONE         => $customer->user->getPhone(),
            MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
            MyEmail::ORDER_NOTE             => $appoint->note,
            MyEmail::ORDER_DATE             => $appoint->formatDate(),
            MyEmail::ORDER_TIME             => $appoint->startTime,
            MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
            MyEmail::ORDER_STAFF            => $appoint->author->getName(),
            MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
            MyEmail::ORDER_SERVICE_LNG      => $appoint->service->longitude,
            MyEmail::ORDER_SERVICE_LAT      => $appoint->service->latitude,
            MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
            MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
            MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
            MyEmail::ORDER_TAX              => '0', // $tax,
            MyEmail::ORDER_DISCOUNT         => $discount, // $discount,
            MyEmail::ORDER_TOTAL_MONEY      => $total_money, // $total
            MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
//            MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
            MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}",
            MyEmail::REDEMPTION_CODE        => $redeemcode,
        );

        if ($options)
        {
            $order_key = $options['order_key'];
        }
        else
        {
            $order_key = NULL;
        }

        Redeem::set_redeem_from_paypal(array(
            'redeem_code' => $redeemcode,
            'biz_ukey'    => $appoint->service->user->ukey,
            'biz_id'      => $appoint->service->user->id,
            'order_key'   => $order_key,
            'service_id'  => $appoint->service->sid,
            'amount'      => $total_money,
        ));


        ob_start();
        $mail->send();
        ob_clean();
    }

    /*
     * Notify new appoint to Staff
     */

    public static function NewAppointToStaff($appoint, $customer, $isc_file)
    {
        $mail = new MyEmail();

        $sendmail   = true;
        $mail->type = MyEmail::TYPE_NEW_APPOINT_NOTI_STAFF;

        if ($sendmail)
        {
            //Mail to Staff
            $mail->to = $appoint->author->email;
            $mail->addAttach($isc_file);

            $mail->params = array(
                MyEmail::CUSTOMER_NAME         => $customer->user->getName(),
                MyEmail::CUSTOMER_PHONE        => $customer->user->getPhone(),
                MyEmail::CUSTOMER_EMAIL        => $customer->user->email,
                MyEmail::ORDER_NOTE            => $appoint->note,
                MyEmail::ORDER_DATE            => $appoint->formatDate(),
                MyEmail::ORDER_TIME            => $appoint->startTime,
                MyEmail::ORDER_SERVICE         => $appoint->service->s_name,
                MyEmail::ORDER_STAFF           => $appoint->author->getName(),
                MyEmail::ORDER_PRICE           => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_LNG     => $appoint->service->longitude,
                MyEmail::ORDER_SERVICE_LAT     => $appoint->service->latitude,
                MyEmail::ORDER_SERVICE_IMAGE   => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS => $appoint->service->address,
                MyEmail::CODE_BUSINESS         => $appoint->service->user->business_name,
                MyEmail::ORDER_TAX             => '0', // $tax,
                MyEmail::ORDER_DISCOUNT        => '0', // $discount,
                MyEmail::ORDER_TOTAL_MONEY     => $appoint->service->getPriceFormat(), // $total,
            );
            ob_start();
            $mail->send();
            ob_clean();
        }
    }

    /*
     * Notify change appoint email to Staff
     */

    public static function ChangeAppointToStaff($appoint, $customer, $isc_file)
    {
        $mail = new MyEmail();

        $sendmail   = true;
        $mail->type = MyEmail::TYPE_CHANGE_APPOINT_NOTI_STAFF;

        if ($sendmail)
        {
            //Mail to Staff
            $mail->to = $appoint->author->email;
            $mail->addAttach($isc_file);

            $mail->params = array(
                MyEmail::CUSTOMER_NAME          => @$customer->user->getName(),
                MyEmail::CUSTOMER_PHONE         => @$customer->user->getPhone(),
                MyEmail::CUSTOMER_EMAIL         => @$customer->user->email,
                MyEmail::ORDER_NOTE             => @$appoint->note,
                MyEmail::ORDER_DATE             => @$appoint->formatDate(),
                MyEmail::ORDER_TIME             => @$appoint->startTime,
                MyEmail::ORDER_SERVICE          => @$appoint->service->s_name,
                MyEmail::ORDER_STAFF            => @$appoint->author->getName(),
                MyEmail::ORDER_PRICE            => @$appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
                MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
                MyEmail::ORDER_TAX              => 0, //$tax,
                MyEmail::ORDER_DISCOUNT         => 0, //$discount,
                MyEmail::ORDER_TOTAL_MONEY      => $appoint->service->getPriceFormat(), //$total,
//                MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
                MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}",
                MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
            );
            ob_start();
            $mail->send();
            ob_clean();
        }
    }

    /*
     * Notify change appoint email to Biz
     */

    public static function ChangeAppointToBiz($appoint, $customer, $isc_file)
    {
        $mail = new MyEmail();

        $sendmail   = true;
        $mail->type = MyEmail::TYPE_CHANGE_APPOINT_NOTI_BIZ;

        if ($sendmail)
        {
            //Mail to Biz
            $mail->to = $appoint->service->user->email;
            $mail->addAttach($isc_file);

            $mail->params = array(
                MyEmail::CUSTOMER_NAME          => $customer->user->getName(),
                MyEmail::CUSTOMER_PHONE         => $customer->user->getPhone(),
                MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
                MyEmail::ORDER_NOTE             => $appoint->note,
                MyEmail::ORDER_DATE             => $appoint->formatDate(),
                MyEmail::ORDER_TIME             => $appoint->startTime,
                MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
                MyEmail::ORDER_STAFF            => $appoint->author->getName(),
                MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
                MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
                MyEmail::ORDER_TAX              => '0', //$tax,
                MyEmail::ORDER_DISCOUNT         => '0', // $discount,
                MyEmail::ORDER_TOTAL_MONEY      => $appoint->service->getPriceFormat(), // $total,
//                MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
                MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}",
                MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
            );

            ob_start();
            $mail->send();
            ob_clean();
        }
    }

    /*
     * Notify change appoint email to Cus
     */

    public static function ChangeAppointToCus($appoint, $customer, $isc_file)
    {
        $mail = new MyEmail();

        $sendmail   = true;
        $mail->type = MyEmail::TYPE_CHANGE_APPOINT_NOTI_CUS;


        if ($sendmail)
        {
            //Mail to Biz
            $mail->to = $customer->user->email;
            $mail->addAttach($isc_file);

            $mail->params = array(
                MyEmail::CUSTOMER_NAME          => $customer->user->getName(),
                MyEmail::CUSTOMER_PHONE         => $customer->user->getPhone(),
                MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
                MyEmail::ORDER_NOTE             => $appoint->note,
                MyEmail::ORDER_DATE             => $appoint->formatDate(),
                MyEmail::ORDER_TIME             => $appoint->startTime,
                MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
                MyEmail::ORDER_STAFF            => $appoint->author->getName(),
                MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
                MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
                MyEmail::ORDER_TAX              => '0', //$tax,
                MyEmail::ORDER_DISCOUNT         => '0', // $discount,
                MyEmail::ORDER_TOTAL_MONEY      => $appoint->service->getPriceFormat(), //$total,
//                MyEmail::LINK_MAP => 'https://www.google.com/maps/place/' . 'Singapore+' . $appoint->service->user->postal_code,
                MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}",
                MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->getPhone(),
            );

            ob_start();
            $mail->send();
            ob_clean();
        }
    }

    /**
     * Cancel Appointment Email Notifycation
     * @param type $appoint
     * @param type $customer
     * @param type $toGroupType
     */
    public static function CancelAppoint($appoint, $customer, $toGroupType = Users::TYPE_CUSTOMER)
    {
        $mail = new MyEmail();

        $sendmail = true;
        //Switch user type
        switch ($toGroupType)
        {
            case Users::TYPE_BUSINESS:
                $mail->type = MyEmail::TYPE_CANCEL_APPOINT_NOTI_BIZ;
                $mail->to   = $appoint->service->user->email;
                break;
            case Users::TYPE_STAFF:
                $mail->type = MyEmail::TYPE_CANCEL_APPOINT_NOTI_STAFF;
                $mail->to   = $appoint->author->email;
                break;
            default :
                $mail->type = MyEmail::TYPE_CANCEL_APPOINT_NOTI_CUS;
                $mail->to   = $customer->user->email;
                break;
        }

        if ($sendmail)
        {
            $order = @$appoint->order;

            if ($order)
            {
                $order_tax = 0; //$order->tax ? $order->tax : 0;
                $tax_money = round($order_tax * $appoint->service->price / 100, 2);
                $tax_money = number_format($tax_money, 2);

                $tax = '$' . $tax_money . ' (' . $order_tax . '%)';

                $order_discount = $order->hotdeal_discount ? $order->hotdeal_discount : 0;
                $discount_money = round($order_discount * $appoint->service->price / 100, 2);
                $discount_money = number_format($discount_money, 2);
                $char           = $discount_money == 0 ? '' : '-';
                $discount       = '$' . $discount_money . " ($char" . $order_discount . '%)';

                $total = number_format($appoint->service->price - $discount_money + $tax_money, 2);
            }
            else
            {
                $discount = 0;
                $total    = $appoint->service->price;
                $tax      = 0;
            }



            $mail->params = array(
                MyEmail::CUSTOMER_NAME          => $customer->user->getName(),
                MyEmail::CUSTOMER_PHONE         => $customer->user->getPhone(),
                MyEmail::CUSTOMER_EMAIL         => $customer->user->email,
                MyEmail::ORDER_NOTE             => $appoint->note,
                MyEmail::ORDER_DATE             => $appoint->formatDate(),
                MyEmail::ORDER_TIME             => $appoint->startTime,
                MyEmail::ORDER_SERVICE          => $appoint->service->s_name,
                MyEmail::ORDER_STAFF            => $appoint->author->getName(),
                MyEmail::ORDER_PRICE            => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_LNG      => $appoint->service->longitude,
                MyEmail::ORDER_SERVICE_LAT      => $appoint->service->latitude,
                MyEmail::ORDER_SERVICE_IMAGE    => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS  => $appoint->service->address,
                MyEmail::CODE_BUSINESS          => $appoint->service->user->business_name,
                MyEmail::MERCHANT_CONTACT_PHONE => $appoint->service->user->office_phone,
                MyEmail::ORDER_TAX              => $tax,
                MyEmail::ORDER_DISCOUNT         => $discount,
                MyEmail::ORDER_TOTAL_MONEY      => $total,
                MyEmail::LINK_MAP               => "http://maps.google.com/maps?q={$appoint->service->latitude},{$appoint->service->longitude}"
            );

            ob_start();
            $mail->send();
            ob_clean();
        }
    }

    /**
     * Send invite review email
     * @param type $appoint
     * @param type $customer
     * @param type $toGroupType
     */
    public static function inviteReview($appoint, $customer)
    {
        $mail     = new MyEmail();
        $sendmail = true;

        $mail->type = MyEmail::TYPE_INVITE_REVIEW;
        $mail->to   = $customer->user->email;

        $code = md5(self::SECRET_KEY);

        //$review_url = Yii::app()->createAbsoluteUrl('frontend/site/review', array(
        $review_url = Yii::app()->createAbsoluteUrl('frontend/review/index', array(
            //'service' => $appoint->service_id,
            'apt_id' => $appoint->apt_id,
            'ukey'   => $customer->ckey
//            'code' => $code,
        ));
        if ($sendmail)
        {
            $mail->params = array(
                MyEmail::REVIEWS_SERVICE_LINK  => $review_url,
                MyEmail::CODE_USER_NAME        => $customer->user->getName(),
                MyEmail::CUSTOMER_NAME         => $customer->user->getName(),
                MyEmail::CUSTOMER_PHONE        => $customer->user->getPhone(),
                MyEmail::CUSTOMER_EMAIL        => $customer->user->email,
                MyEmail::ORDER_NOTE            => $appoint->note,
                MyEmail::ORDER_DATE            => $appoint->formatDate(),
                MyEmail::ORDER_TIME            => $appoint->startTime,
                MyEmail::ORDER_SERVICE         => $appoint->service->s_name,
                MyEmail::ORDER_STAFF           => $appoint->author->getName(),
                MyEmail::ORDER_PRICE           => $appoint->service->getPriceFormat(),
                MyEmail::ORDER_SERVICE_LNG     => $appoint->service->longitude,
                MyEmail::ORDER_SERVICE_LAT     => $appoint->service->latitude,
                MyEmail::ORDER_SERVICE_IMAGE   => $appoint->service->get_absolute_image_url_by_size(600, 340),
                MyEmail::ORDER_SERVICE_ADDRESS => $appoint->service->address,
                MyEmail::CODE_BUSINESS         => $appoint->service->user->business_name
            );

            ob_start();
            $mail->send();
            ob_clean();
        }
    }

}

/** 
 * End file MyEmail
 */

